package twentytwenty;

/**
 * @Author: Spider Man
 * @Description: 67.二进制求和
 * @Date Created in 2020-09-24 12:29
 * @Modified By:
 */
public class AddBinary_67 {
    public static void main(String[] args) {
        String  a = "1111", b = "100";
        System.out.println(addBinary(a, b));
        char shit = '0';
        char fuck = '1';
        System.out.println(fuck - shit);
    }

    public static String addBinary(String a, String b) {
        StringBuilder ans = new StringBuilder();

        int n = Math.max(a.length(), b.length()), carry = 0;
        for (int i = 0; i < n; ++i) {
            carry += i < a.length() ? (a.charAt(a.length() - 1 - i) - '0') : 0;
            carry += i < b.length() ? (b.charAt(b.length() - 1 - i) - '0') : 0;
            ans.append((char) (carry % 2 + '0'));
            carry /= 2;
        }

        if (carry > 0) {
            ans.append('1');
        }

        ans.reverse();

        return ans.toString();
    }
}
